package com.zr.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.zr.constant.MessageConstant;
import com.zr.entity.PageResult;
import com.zr.entity.QueryPageBean;
import com.zr.entity.Result;
import com.zr.pojo.CheckGroup;
import com.zr.service.CheckGroupService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @auther zhaorui
 * @date 2019-11-24 - 0:05
 */
@RestController
@RequestMapping("/checkGroup")
public class CheckGroupController {

    @Reference
    private CheckGroupService checkGroupService;

    /**
     * 添加检查组
     * @param checkGroup
     * @return
     */
    @RequestMapping("/add")
    public Result add(@RequestBody CheckGroup checkGroup , Integer[] checkitemIds){
        try {
            checkGroupService.add(checkGroup,checkitemIds);
            return Result.ok(MessageConstant.ADD_CHECKGROUP_SUCCESS,null);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.ok(MessageConstant.ADD_CHECKGROUP_FAIL);
        }
    }



    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody  QueryPageBean queryPageBean){

        return checkGroupService.findPage(queryPageBean);

    }

    /**
     * 根据id查询检查组
     * @param id
     * @return
     */
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            CheckGroup checkGroup = checkGroupService.findById(id);
            return Result.ok(MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkGroup);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }


    /**
     * 根据CheckGroup的id查找CheckItemId
     * @param id
     * @return
     */
    @RequestMapping("/findCheckItemIdByCheckGroupId")
    public Result findCheckItemIdByCheckGroupId(Integer id){
        try {
            List<Integer> checkitemIds = checkGroupService.findCheckItemIdByCheckGroupId(id);
            return Result.ok(MessageConstant.QUERY_CHECKITEM_SUCCESS,checkitemIds);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

    /**
     * 编辑检查组
     * @param checkGroup
     * @param checkitemIds
     * @return
     */
    @RequestMapping("/edit")
    public Result edit(@RequestBody CheckGroup checkGroup , Integer[] checkitemIds){
        try {
            checkGroupService.edit(checkGroup,checkitemIds);
            return Result.ok(MessageConstant.EDIT_CHECKGROUP_SUCCESS,null);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(MessageConstant.EDIT_CHECKGROUP_SUCCESS);

        }
    }

    @RequestMapping("/findAll")
    public Result findAll(){
        try {
            List<CheckGroup> checkGroupList = checkGroupService.findAll();
            return Result.ok(MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkGroupList);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }




}
